/*
 * @Author: yc
 * @Date: 2020-05-26 20:31:00
 * @LastEditTime: 2020-05-31 12:18:40
 * @FilePath: \react-admin\src\requests\index.js
 */
import axios from 'axios'
import { message } from 'antd'

// const isDev = process.env.NODE_ENV === 'development'

const service = axios.create({
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  }
})

// 请求拦截
service.interceptors.request.use((config) => {
  // config.headers['authToken'] = 'sdsdsa'
  return config
})

// 响应拦截
service.interceptors.response.use((resp) => {
  const res = resp.data
  if(res.code === 0) {
    return res
  } else {
    // 全局处理错误
    message.error(res.msg)
    return Promise.reject('error')
  }
})

// 对错误进行处理
 const awaitWrap = (options) => {
  return service(options)
    .then(res => ({ data: res.data}) )
    .catch(err => ({ err }))
}

export default awaitWrap